/*
 * SIT_ScrollBar.c : vertical or horizontal proportionnal scrollbar
 *
 * Written by T.Pierron, Nov 30, 2008.
 */

#include "SIT_P.h"
#include <stdio.h>

	TagList SBClass[] = {
		{ SIT_SuperClass,  NULL,          ___, SIT_INT,  (ULONG) WidgetClass },
		{ SIT_MinValue,    "minValue",    _SG, SIT_INT,  OFFSET(SIT_ScrollBar, sb_Info.nMin) },
		{ SIT_MaxValue,    "maxValue",    _SG, SIT_INT,  OFFSET(SIT_ScrollBar, sb_Info.nMax) },
		{ SIT_PageSize,    "pageSize",    _SG, SIT_INT,  OFFSET(SIT_ScrollBar, sb_Info.nPage) },
		{ SIT_LineHeight,  "lineHeight",  _SG, SIT_INT,  OFFSET(SIT_ScrollBar, sb_LineHeight) },
		{ SIT_ScrollPos,   "scrollPos",   _SG, SIT_INT,  OFFSET(SIT_ScrollBar, sb_Info.nPos) },
		{ SIT_HorizScroll, "horizScroll", C__, SIT_BOOL, OFFSET(SIT_ScrollBar, sb_IsHoriz) },
		{ SIT_IsDragged,   NULL,          __G, SIT_BOOL, OFFSET(SIT_ScrollBar, sb_IsDragged) },
		{ SIT_TagEnd }
	};

Bool SIT_HandleScrollBar(SIT_Widget, int type);

/* Group modifications (min, max, pos, page) into one call for smoothness */
static int SIT_PostProcessSB(SIT_Widget w, APTR call_data, APTR user_data)
{
	SIT_ScrollBar sb = (SIT_ScrollBar) w;

	if (sb->sb_Info.fMask)
	{
		SetScrollInfo(w->sw_Handle, SB_CTL, &sb->sb_Info, w->sw_Visible > 0);
		sb->sb_Info.fMask = 0;
		w->sw_PostProcess = NULL;
//		if (HAS_EVT(w, SITE_OnScroll))
//			SIT_HandleScrollBar(w, -1);
	}
	return 0;
}

static int SIT_SetScrollBarValues(SIT_Widget w, APTR call_data, APTR user_data)
{
	TagList *     tag   = call_data;
	SIT_Variant * value = user_data;
	SIT_ScrollBar sb    = (SIT_ScrollBar) w;
	int           isSet = 0;

	switch (tag->tl_TagID) {
	case SIT_MinValue:    sb->sb_Info.nMin  = value->integer; isSet = SIF_RANGE; break;
	case SIT_MaxValue:    sb->sb_Info.nMax  = value->integer; isSet = SIF_RANGE; break;
	case SIT_PageSize:    sb->sb_Info.nPage = value->integer; isSet = SIF_PAGE;  break;
	case SIT_ScrollPos:   sb->sb_Info.nPos  = value->integer; isSet = SIF_POS;   break;
	case SIT_HorizScroll: sb->sb_IsHoriz    = value->integer; break;
	default: return SIT_SetWidgetValue(w, call_data, user_data); // super class
	}

	if (isSet)
	{
		sb->sb_Info.fMask |= isSet;
		sb->sb_Info.cbSize = sizeof sb->sb_Info;
		w->sw_PostProcess = SIT_PostProcessSB;
	}
	return 0;
}

/* Set minimal width/height */
static int SIT_MeasureScrollBar(SIT_Widget w, APTR call_data, APTR user_data)
{
	SIT_ScrollBar sb  = (SIT_ScrollBar) w;
	SIZE *        ret = call_data;

	if (sb->sb_IsHoriz) ret->cy = GetSystemMetrics(SM_CYHSCROLL);
	else                ret->cx = GetSystemMetrics(SM_CXVSCROLL);

	return 0;
}

/* Control creation */
BOOL SIT_CreateScrollBar(SIT_Widget w, va_list args)
{
	SIT_ScrollBar sb = (SIT_ScrollBar) w;
	HWND          frame;
	int           style = WS_CHILD;

	/* Subclass SetValues */
	w->sw_SetValue = SIT_SetScrollBarValues;
	w->sw_OptimalWidth = SIT_MeasureScrollBar;

	/* Default values */
	sb->sb_LineHeight = 1;
	SIT_ParseTags(w, args, w->sw_Class = SBClass);

	if (HAS_TAB(w))   style |= WS_TABSTOP;
	if (IsVisible(w)) style |= WS_VISIBLE;

	/* Create Win32 control, pos and dim will be set later */
	frame = w->sw_Handle = CreateWindow(WC_SCROLLBAR, w->sw_Name,
		style | (sb->sb_IsHoriz ? SBS_HORZ : SBS_VERT),
		0, 0, 10, 10, SIT_GetContainer(w), (HMENU) MAKE_ID(w), instance, NULL);

	if (frame == NULL) return FALSE;
	SIT_SetDefaultFont(w);

	if (w->sw_PostProcess)
		w->sw_PostProcess(w, NULL, NULL);

	SetProp(frame, sit_class, (HANDLE) w);

	return TRUE;
}
